package com.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import com.module.commonui.R;

public class TGCicleView extends View {
    private float mPenStokeWidth = 8.0f;
    private int mColor = Color.BLACK;
    private int mStyle = Paint.Style.STROKE.ordinal();
    private float mRadius = 200f;
    private Paint mPaint;

    public TGCicleView(Context context) {
        this(context, null);
    }

    public TGCicleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TGCicleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TGCicleView);
        mColor = typedArray.getColor(R.styleable.TGCicleView_cicleColor, Color.BLACK);
        mRadius = typedArray.getDimension(R.styleable.TGCicleView_cicleRadius, 0);
        mStyle = typedArray.getInt(R.styleable.TGCicleView_cicleStyle, Paint.Style.STROKE.ordinal());
        mPenStokeWidth = typedArray.getFloat(R.styleable.TGCicleView_penStokeWidth, 8.0f);
        typedArray.recycle();
    }


    // 重写draw方法
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mPaint = new Paint();
        mPaint.setColor(mColor);
        Paint.Style style = Paint.Style.values()[mStyle];
        mPaint.setStyle(style);//画笔属性是空心圆
        mPaint.setStrokeWidth(mPenStokeWidth);//设置画笔粗细

        /*四个参数：
          参数一：圆心的x坐标
          参数二：圆心的y坐标
          参数三：圆的半径
          参数四：定义好的画笔
        */
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mPaint);

    }

}